package com.example.mvvm.ui

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.Observer
import androidx.lifecycle.OnLifecycleEvent
import com.example.mvvm.R
import com.example.mvvm.databinding.FragmentRecommendBinding
import com.example.mvvm.ui.my.MyViewModel
import com.example.mvvm.viewmodel.HomeViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ReCommendFragment : Fragment(), LifecycleObserver {
    val homeViewModel: HomeViewModel by viewModels()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        print("onCreateView")
        Log.e("wode", "onCreateView")
        /*
        其中绑定类命名规则：布局文件名首字母大写且省去下划线+Binding）
         */
        val inflate = DataBindingUtil.inflate<FragmentRecommendBinding>(
            inflater,
            R.layout.fragment_recommend,
            container,
            false
        )
        /*
        三种种方法获取model
        1.通过ViewModelProvider
        2.通过dagger
        https://blog.csdn.net/cherry459/article/details/84971886
        先在model构造函数上加@Inject、再写一个component、build一下。
        3.通过hilt，这样dagger就不用写component了，
        通过var homeViewModel: HomeViewModel by viewModel()
        在model构造函数上加@ViewModelInject
        val homeViewModel: HomeViewModel by viewModels()
         */

        /*val homeViewModel = ViewModelProvider(
            this,
            ViewModelProvider.NewInstanceFactory()
        ).get(HomeViewModel::class.java)//HomeViewModel::class.java就是获取model的class
        */

//        DaggerModelComponent.builder().build().inject(this)


        val tvTest: TextView = inflate.root.findViewById<TextView>(R.id.tv_test)
        val btTest = inflate.root.findViewById<Button>(R.id.bt_test);
        btTest.setOnClickListener(View.OnClickListener {
            homeViewModel.setP("1111")
        })
        homeViewModel.setP("000")
        tvTest.text = homeViewModel.getP()
        homeViewModel.pa.observe(viewLifecycleOwner, Observer {
            tvTest.text = homeViewModel.getP()
        })
        Log.e("wode", "开始")
        GlobalScope.launch {
            val requestMy = requestMy()
            Log.e("wode", "下面继续")
            Log.e("wode", "requestmy===$requestMy")
        }
        /*
        1、什么是协程：就是开启一个轻量级的线程
        2、协程挂起suspend:就是开启一个线程，完成之后再切换回原线程，提示作用
        3、非阻塞式：就是协程用阻塞式语言，完成非阻塞式代码，本质和thread是一样的
        */
        /*GlobalScope.launch {
            withContext(Dispatchers.IO){
                //网络请求
                request()
            }
            initUI()
        }*/
        /*
        GlobalScope是线程级别的，当activity或者fragment销毁的，协程里面的耗时操作不会停止，所以推荐用lifecycleScope
         */
        return inflate.root

//        return inflater.inflate(R.layout.fragment_recommend,container,false)


    }

    private suspend fun requestMy(): String {
        delay(3000)
        Log.e("wode", "1111111")
        return "2222"
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun myOnStar() {
        print("onStar")
    }


    override fun onAttach(context: Context) {
        super.onAttach(context)
        println("onAttach")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        print("onCreate")
    }

}
